<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection" :dataSource="dataSource">
      <template #tableTitle>
        <a-button type="primary" preIcon="ant-design:plus-outlined" @click="handleAdd">新增</a-button>
      </template>
      <template #executeCount="{ record }">
        <a-switch v-model:checked="record.enable" @click="handleRunChange(record)" />
      </template>
      <template #action="{ record }">
        <TableAction :actions="getActions(record)" />
      </template>
    </BasicTable>
    <AlarmModal @register="registerModal" @success="reload" />
  </div>
</template>
<script lang="ts" name="alarm-list" setup>
  import { reactive } from 'vue';
  import { BasicTable, TableAction } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { list, run, deleteAlarm } from './alarm.api';
  import { columns, searchFormSchema } from './alarm.data';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { useMessage } from '/@/hooks/web/useMessage';
  import AlarmModal from './AlarmModal.vue';
  const [registerModal, { openModal }] = useModal();
  const { createMessage: $message } = useMessage();

  // 列表页面公共参数、方法
  const { tableContext } = useListPage({
    tableProps: {
      title: '告警管理',
      //   api: list,
      columns: columns,
      showIndexColumn: true,
      formConfig: {
        schemas: searchFormSchema,
      },
    },
  });

  const [registerTable, { reload }] = tableContext;

  const dataSource = reactive([
    {
      time: '2024-03-06 17:57:20',
      id: 'a3d0a32faa23485abda363875350ab13',
      name: '123',
      desc: '123',
      enable: true,
      alertTemplateType: 'TEXT',
      messageWay: 'WECHAT_ROBOT',
      hook: '123',
      alarmType: 'MEMORY_LOAD_ALARM',
    },
    {
      time: '2024-01-18 08:40:28',
      id: '33004dc4551044e98f5970438219c102',
      name: '1',
      desc: '1',
      enable: false,
      alertTemplateType: 'TEXT',
      messageWay: 'WECHAT_ROBOT',
      hook: '1',
      alarmType: 'MEMORY_LOAD_ALARM',
    },
  ]);

  /**
   * 操作列定义
   * @param record
   */
  function getActions(record) {
    return [
      {
        label: '编辑',
        onClick: handleEdit.bind(null, record),
        ifShow: () => {
          return !record.enable;
        },
      },
      {
        label: '删除',
        popConfirm: {
          title: '是否确认删除',
          confirm: handleDelete.bind(null, record),
        },
        ifShow: () => {
          return !record.enable;
        },
      },
    ];
  }

  /**
   * 新增事件
   */
  function handleAdd() {
    openModal(true, {
      isUpdate: false,
      showFooter: true,
    });
  }

  /**
   * 编辑事件
   */
  async function handleEdit(record: Recordable) {
    openModal(true, {
      record,
      isUpdate: true,
      showFooter: true,
    });
  }

  /**
   * 删除事件
   */
  async function handleDelete(record) {
    // await deleteAlarm({ id: record.id }, reload);
  }

  async function handleRunChange(record) {
    let params: any = {};
    params.key = record.id;
    params.enable = !record.enable;
    await run(params, reload);
  }
</script>
